package com.gz.furnitures.service.impl;

import com.gz.furnitures.dao.FurnDao;
import com.gz.furnitures.dao.impl.FurnDaoImpl;
import com.gz.furnitures.entity.Furn;
import com.gz.furnitures.entity.Page;
import com.gz.furnitures.service.FurnsService;

import java.util.List;

public class FurnServiceImpl implements FurnsService {
    FurnDaoImpl furnDao=new FurnDaoImpl();
    @Override
    public List<Furn> queryFurns() {
        return furnDao.queryFurns();
    }

    @Override
    public int addFurn(Furn furn) {
        return furnDao.addFurn(furn);
    }

    public int deleteFurnById(int Id) {
        return furnDao.deleteFurnById(Id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDao.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDao.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow=furnDao.getTotalRow();
        page.setTotalRow(totalRow);
        int pageTotalCount=totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        //这里的begin需要计算
        //注意这里有一个坑
        int begin=(pageNo-1)*pageSize;
        List<Furn> pageItems = furnDao.getPageItems(begin, pageSize);
        page.setItems(pageItems);
        //url,分页导航
        return page;
    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow=furnDao.getTotalRowByName(name);
        page.setTotalRow(totalRow);
        int pageTotalCount=totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        //这里的begin需要计算
        //注意这里有一个坑
        int begin=(pageNo-1)*pageSize;
        List<Furn> pageItems = furnDao.getPageItemsByName(begin, pageSize, name);
        page.setItems(pageItems);
        //url,分页导航
        return page;
    }

}
